apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service"], function(V, $api, $, app, service) {
      app.ready();
        var vm = {
            el: '.container',
            data: {
                commentList: '',
                tabIndex: 0,
                score: 0, //0为全部，1为差评，2为中评，3为好评
                reply_images: '',
                page: 1,
                show_reply: [],
                seller_name: $api.getStorage("user").data.nickname,
                buyer_name: '',
                comment_id: '',
                all_num: '0',
                good_num: '0',
                middle_num: '0',
                bad_num: '0',
            },
            mounted: function() {

                if (api.systemType == 'ios') {
                    //--解决ios输入法挡住输入框的问题
                    $(document).on("focus", "#replyInput", function() {
                        console.log($('body').scrollTop());
                        var top = $('body').scrollTop();
                        setTimeout(function() {
                            if (top >= 147) {
                                window.scrollTo(0, top - 147);
                            } else {
                                window.scrollTo(0, top);
                            }
                        }, 50);
                    });

                    $(document).on("blur", "#replyInput", function() {
                        setTimeout(function() {
                            $('.reply').css({
                                'display': 'none'
                            });
                        }, 500);
                    });

                } else {
                    $('.reply').css({
                        'position': 'fixed',
                        'bottom': 0
                    })
                }

                api.addEventListener({
                    name: 'keyback'
                }, function(ret, err){
                    api.sendEvent({
                        name: 'getShopRedTip'
                    });
                    api.closeWin()
                });
            },
            methods: {
                //--获取顾客评论列表
                getCommentList: function() {
                    var that = this;

                    app.ajax(service['getCommentList'], {
                            data: {
                                values: {
                                    shop_code: api.pageParam.shopId,
                                    unionid: $api.getStorage("shop_unionid"),
                                    state: this.score, //0为全部，1为差评，2为中评，3为好评
                                    page: this.page,
                                }
                            }
                        },
                        function(res) {
                            // var shopCode = res.data.shop_code; // 商店id
                            if (res.code) {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            } else {
                                if (res.data.comment_list.length == 0) {
                                    if (that.page != 1) {
                                        that.page = parseInt(that.page - 1);
                                        api.toast({
                                            msg: '没有更多数据...',
                                            duration: 2000,
                                            location: 'middle'
                                        });
                                    } else {
                                        that.commentList = [];
                                        $(".comment-list").css("display", "none");
                                        $(".comment_manage_null").css("display", "block");
                                    }
                                } else {
                                    that.show_reply = [];
                                    for (var i = 0; i < res.data.comment_list.length; i++) {
                                        res.data.comment_list[i]['star_arr'] = [];
                                        for (var j = 0; j < res.data.comment_list[i].star_num; j++) {
                                            res.data.comment_list[i]['star_arr'].push('1');
                                        }

                                        if (res.data.comment_list[i].reply_content) {
                                            that.show_reply[i] = 1;
                                        } else {
                                            that.show_reply[i] = 0;
                                        }
                                    }
                                    that.commentList = res.data.comment_list;
                                    console.log(JSON.stringify(that.commentList))
                                    that.all_num = res.data.all_num;
                                    that.good_num = res.data.good_num;
                                    that.middle_num = res.data.middle_num;
                                    that.bad_num = res.data.bad_num;
                                    console.log("获取顾客评论列表成功!");
                                    $(".comment-list").css("display", "block");
                                    $(".comment_manage_null").css("display", "none");
                                }
                            }
                        }
                    );
                },
                //--商家回复评论
                writeComment: function(comment_id, nickname) {
                    var that = this;
                    this.comment_id = comment_id;
                    this.buyer_name = nickname;

                    if (api.systemType == 'ios') {
                        console.log($('body').scrollTop());
                        var top = $('body').scrollTop();
                        if (top >= 147) {
                            $('.reply').css('top', top + 180);
                        } else {
                            $('.reply').css('top', top + 327);
                        }
                    }
                    // $(".reply").css("top", top);
                    $(".reply").css("display", "block");
                    $('.reply').css('opacity', '1');

                    $("#replyInput").focus() //自动获取焦点
                    $("#replyInput").attr('placeholder', '回复' + this.buyer_name)
                },
                //--监听input输入
                valid: function() {
                    this.$nextTick(() => {
                        var that = this;
                        var string = $api.trim($("#replyInput").val());
                        if (string != '') {
                            $("#submit").css("display", "none")
                            $("#sub").css("display", "inline-block")
                        } else {
                            $("#submit").css("display", "inline-block")
                            $("#sub").css("display", "none")
                        }
                    })

                },
                //--商家回复评论接口调用
                sellerOrderComment: function() {
                    var that = this;

                    var reply_content = $("#replyInput").val();
                    $("#replyInput").focus() //自动获取焦点
                    console.log('dianji');

                    app.ajax(service['sellerOrderComment'], {
                            data: {
                                values: {
                                    shop_code: api.pageParam.shopId,
                                    unionid: $api.getStorage("shop_unionid"),
                                    comment_id: this.comment_id,
                                    reply_content: reply_content,
                                    reply_images: '',
                                }
                            }
                        },
                        function(res) {
                            // var shopCode = res.data.shop_code; // 商店id
                            if (res.code) {
                                api.toast({
                                    msg: res.message,
                                    duration: 2000,
                                    location: 'middle'
                                });
                            } else {
                                console.log(JSON.stringify(res.data))
                                console.log("商家回复评论成功!");
                                that.getCommentList();
                            }
                        }
                    );

                    $(".reply").css("display", "none");
                    $("#replyInput").blur();


                },
                tab: function(indes) {
                    this.tabIndex = indes;
                    if (indes == 0) {
                        this.score = 0;
                    } else if (indes == 1) {
                        this.score = 3;
                    } else if (indes == 2) {
                        this.score = 2;
                    } else if (indes == 3) {
                        this.score = 1;
                    }

                    this.getCommentList();
                },

                // 触底
                scrolltobottom: function() {
                    var that = this;
                    this.page = parseInt(this.page + 1);

                    this.getCommentList();
                }
            },
            created: function() {
                var that = this;
                this.getCommentList();

                api.addEventListener({
                    name: 'scrolltobottom',
                    extra: {
                        threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
                    }
                }, function(ret, err) {
                    that.scrolltobottom()
                });

                api.addEventListener({
                    name: 'keyboardshow'
                }, function(ret, err) {
                    $('.header').css({
                        'position': 'static'
                    });
                    $('.comment-label').css({
                        'position': 'static'
                    });
                    $(".container.top-container").css("cssText", "padding-top: 0rem !important;");
                    $('.comment_manage_null').css({
                        'padding-top': '0'
                    });
                });

                api.addEventListener({
                    name: 'keyboardhide'
                }, function(ret, err) {
                    $('.header').css({
                        'top': 0,
                        'position': 'fixed'
                    });
                    $('.comment-label').css({
                        'position': 'fixed'
                    });
                    $(".container.top-container").css("cssText", "padding-top: 3rem !important;");
                    $('.comment_manage_null').css({
                        'position': '80px'
                    });
                    $('.reply').css('opacity', '0');
                });

                //下拉刷新
                app.refreshHeaderInfo(function() {
                    that.page = 1;
                    that.getCommentList()
                });
            }
        };

        var even = function() {

        };
        var newVue = new Vue(vm);
        even();

    })
}
function backPage(){
  api.sendEvent({
      name: 'getShopRedTip'
  });
  api.closeWin()
}
